from sqlalchemy import Column, Integer, String, Text, Enum, Boolean, DateTime, func, JSON
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
DB_ENMU = Enum('Bcenter', 'Omscenter', 'Listingcenter', 'Icenter', 'Pcenter', 'Gcenter', name='db_enum')
LEVEL_ENMU = Enum('NORMAL', 'URGENT', 'CRITICAL', name='alert_level_enum')


class AlertRule(Base):
    __tablename__ = 'alert_rules'

    id = Column(Integer, primary_key=True, index=True)
    rule_name = Column(String(100), nullable=False, comment='规则名称')
    task_group = Column(String(50), default='hourly', comment='所属任务组')
    origin_db = Column(DB_ENMU, nullable=False,
                       default='Bcenter', comment='检测的DB')
    query_sql = Column(Text, nullable=False, comment='检测SQL')

    operator = Column(Enum('>', '<', '=', '>=', '<=', '!=', name='operator_enum'), nullable=False, comment='比较符')
    standard_type = Column(Enum('SQL', 'FIXED', name='standard_type_enum'), nullable=False, comment='标准值类型')
    dest_db = Column(DB_ENMU, nullable=False,
                     default='Bcenter', comment='标准值的DB')
    standard_value = Column(Text, nullable=False, comment='标准值(SQL或固定值)')
    alert_level = Column(LEVEL_ENMU, nullable=False,
                         default='NORMAL', comment='预警等级')
    owner = Column(String(255), nullable=False, comment='负责人(多个用逗号分隔)')
    is_active = Column(Boolean, default=True, comment='是否启用')
    last_trigger_time = Column(DateTime, comment='上次触发时间')
    created_at = Column(DateTime, default=func.now(), comment='创建时间')
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment='更新时间')

    # 新增频率字段
    frequency = Column(String(50), default='1h',
                       comment='执行频率: 10m(10分钟), 1h(1小时), 1d(1天), daily:01:00(每天1点)')
    timezone = Column(String(50), default='Asia/Shanghai', comment='时区设置')
    skip_weekends = Column(Boolean, default=False, comment='是否跳过周末')
    skip_holidays = Column(Boolean, default=False, comment='是否跳过节假日')


class AlertTaskGroup(Base):
    __tablename__ = 'alert_task_groups'

    id = Column(Integer, primary_key=True, index=True)
    group_name = Column(String(50), nullable=False, unique=True, comment='任务组名称')
    description = Column(String(255), comment='任务组描述')
    schedule_type = Column(Enum('interval', 'cron', name='schedule_type_enum'), default='interval', comment='调度类型')
    schedule_value = Column(JSON, nullable=False, comment='调度配置值')
    is_active = Column(Boolean, default=True, comment='是否启用')
    last_run_time = Column(DateTime, comment='上次运行时间')
    created_at = Column(DateTime, default=func.now(), comment='创建时间')


class AlertHistory(Base):
    __tablename__ = 'alert_history'

    id = Column(Integer, primary_key=True, index=True)
    rule_id = Column(Integer, index=True, nullable=False)
    rule_name = Column(String(100), nullable=False)
    actual_value = Column(String(255), nullable=False, comment='实际值')
    operator = Column(Enum('>', '<', '=', '>=', '<=', '!=', name='operator_enum'), nullable=True, comment='比较符')
    standard_value = Column(String(255), nullable=False, comment='标准值')
    notification_flag = Column(Boolean, default=True, comment='是否需要通知')
    trigger_time = Column(DateTime, default=func.now(), comment='触发时间')
    notification_sent = Column(Boolean, default=True, comment='通知是否已发送')
    notification_method = Column(String(50), comment='通知方式')